# -*- coding:UTF-8 -*-
from cmdb.models import BusinessServerMapping

def add_server_mapping(module_no, equipment_no, use_status, server_port, shutdown_port, jmx_port, outer_mapping_port,
                       outline_port, application_path, configure_path, operator, cursor):
    mapping_insert_sql = 'insert into business_server_mapping set module_no="' + module_no \
                                     + '",equipment_no="' + equipment_no + '",use_status=' + use_status \
                                     + ',server_port="' + server_port + '",shutdown_port="' + shutdown_port \
                                     + '",jmx_port="' + jmx_port + '",outer_mapping_port="' + outer_mapping_port \
                                     + '",outline_port="' + outline_port + '",application_path="' + application_path \
                                     + '",configure_path="' + configure_path  + '",operator="' + operator + '"'
    cursor.execute(mapping_insert_sql)

def filter_server_mapping(module_no):
    businessMappingInfo = BusinessServerMapping.objects.filter(module_no=module_no)
    return businessMappingInfo

def del_server_mapping(module_no, cursor):
    mapping_del_sql = 'delete from business_server_mapping where module_no="' + module_no + '"'
    cursor.execute(mapping_del_sql)

def filter_eno_server_mapping(module_no):
    eRes = BusinessServerMapping.objects.filter(module_no=module_no)
    finalArr = []
    equipmentNos = []
    for singleNo in eRes:
        if singleNo.equipment_no not in equipmentNos:
            finalArr.append(singleNo)
            equipmentNos.append(singleNo.equipment_no)
    return finalArr

def exist_module_server_mapping(equipment_no):
    count = BusinessServerMapping.objects.filter(equipment_no=equipment_no).count()
    if count > 0:
        return True
    return False

def exist_eno_server_mappint(module_no):
    count = BusinessServerMapping.objects.filter(module_no=module_no).count()
    if count > 0:
        return True
    return False